import QtQuick 1.0

ListModel {
    id: root
    default property alias roles: xmlListModel.roles
    property alias source: xmlListModel.source
    property alias query: xmlListModel.query
    property alias namespaceDeclarations: xmlListModel.namespaceDeclarations

    property alias status: xmlListModel.status

    function push(item) {
        append(item);
    }

    property variant __xmlListModel: XmlListModel {
        id: xmlListModel

        property variant keys: []
        onSourceChanged: {
            console.debug('onSourceChanged', source)
        }

        function appendItem(index, count) {
            for (var i = 0; i < count; i++) {
                var item = get(i + index);

                var found = false;
                for (var j = 0; j < root.count; j++) {
                    var item2 = root.get(j);
                    var match = true;
                    for (var k = 0; k < xmlListModel.keys.length; k++) {
                        if (item[xmlListModel.keys[k]] != item2[xmlListModel.keys[k]]) {
                            match = false;
                            break;
                        }
                    }
                    if (match) {
                        found = true;
                        break;
                    }
                }

                if (!found) {
                    push(item)
                }
            }
        }

        onItemsInserted: appendItem(index, count)
    }

    Component.onCompleted: {
        var keys = new Array()
        for (var i = 0; i < roles.length; ++i) {
            if (roles[i].isKey) {
                keys.push(roles[i].name)
            }
        }
        xmlListModel.keys = keys
    }
}
